package com.qkl.xiatang.app.utils;

import org.apache.commons.lang3.StringUtils;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtils {

    public static final String SEP = "/";

    private static SecureRandom RANDOM = new SecureRandom();

    public static String getFileSuffix(String fileName){
        String suffix = "";
        if(StringUtils.isNotEmpty(fileName)){
            int len = fileName.length();
            int lastIndex = fileName.lastIndexOf(".");
            if(lastIndex >= 0 && len > lastIndex){
                suffix = fileName.substring(lastIndex + 1,len);
            }
        }
        if(StringUtils.isEmpty(suffix)){
            suffix = "mp4";
        }
        return suffix;
    }

    public static  String yyyyMMddPath(){
        return new SimpleDateFormat("yyyyMMdd").format(new Date());
    }

    public static String companyPath(String companyId){
        String defaultPath = "common";
        if(StringUtils.isNotEmpty(companyId)){
            defaultPath = companyId;
        }
        return defaultPath;
    }

    public static String genFilePrefix(){
        long currTime = System.currentTimeMillis();
        int ran = RANDOM.nextInt(10000);
        return String.valueOf(currTime) + ran;
    }

    public static String uploadDirPath(String companyId){
        return companyPath(companyId) + SEP + yyyyMMddPath() + SEP;
    }

    public static String toDirPath(String filePath){
        if(StringUtils.isEmpty(filePath)){
            return filePath;
        }
        int lastIndex = StringUtils.lastIndexOf(filePath,SEP);
        if( lastIndex > -1) {
            return filePath.substring(0, lastIndex);
        }
        return filePath;
    }

    public static String toCompanyDir(String companyId,String taskDate){
        return companyId + SEP + taskDate;
    }

    public static void main(String[] args) {
        //System.out.println(toDirPath("1738116933/767970818/2024022717/090224586302339.mp4/"));
    }

}
